package by.zmy.aop;

import java.lang.reflect.Method;
import java.util.function.Predicate;

public class Pointcut {
    private Predicate<Class<?>> classMatcher;
    private Predicate<Method> methodMatcher;

    public Pointcut(Predicate<Class<?>> classMatcher, Predicate<Method> methodMatcher) {
        this.classMatcher = classMatcher;
        this.methodMatcher = methodMatcher;
    }

    public boolean matches(Class<?> clazz, Method method) {
        return classMatcher.test(clazz) && methodMatcher.test(method);
    }

    // 创建基于方法名的切点
    public static Pointcut forMethodName(String methodName) {
        return new Pointcut(c -> true, m -> m.getName().equals(methodName));
    }

    // 创建基于注解的切点
    public static Pointcut forAnnotation(Class<? extends java.lang.annotation.Annotation> annotationType) {
        return new Pointcut(
                c -> true,
                m -> m.isAnnotationPresent(annotationType)
        );
    }
}